L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
print(L[1:3])
# ['Michael', 'Sarah', 'Tracy']

print(L[:3])
# ['Michael', 'Sarah', 'Tracy']

print(L[-2:-1])

L = list(range(100))

print(L[-10:])
# [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

print(L[::5])
# [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

d = {'name':'ZhangSan', 'address':'Shenzhen'}

for key in d:
    print(key)

for val in d.values():
    print(val)

for k, v in d.items():
    print(k+' '+v)



questions = ['name', 'quest','favorite color']

answers = ['lancelot', 'the holy grail', 'yellow']

answers = ['lancelot', 'the holy grail']

for q, a in zip(questions, answers):
    print('What is your {0} ? It is {1}'.format(q,a) )

for i in reversed(range(1,10,2)):
    print(i)

basket = ['banana', 'apple', 'apple']
for f in sorted(set(basket)):
    print(f)

import math
raw_data = [63.22, float('NaN')]
print(raw_data)
filtered_data = []
for v in raw_data:
    if not math.isnan(v):
        filtered_data.append(v)

print(filtered_data)

squares = [x**2 for x in range(1,11)]
print(squares)

squares = [x**2 for x in range(1,11) if x%2==0]
print(squares)

data = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x!=y]
print(data)

import  os
dir = [d for d in os.listdir('.')]
print(dir)
# ['.idea', 'Intro_1.py', 'Python_advanced_features.py', 'Test.py']


L = ['Hello', 'World', 18, 'Apple', None]
L_str = [s.lower() for s in L if isinstance(s,str)]
print(L_str)

from collections import  Iterable
it1 = isinstance([], Iterable)
print(it1)

from collections import Iterator
it2 = isinstance([], Iterator)
print(it2)

def add(x, y, f):
    return f(x) + f(y)

print(add(-10, 5, abs))


def log(text):
    def decorator(func):
        def wrapper(*args, **kv):
            print('%s %s():' % (text, func.__name__))
            return func(*args, **kv)
        return wrapper
    return decorator

@log('execute')
def now():
    print('2015-3-27')

f = now

f()

print(now.__name__)

class Student(object):

    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score(self):
        #print('%s: %s' % (self.name, self.score))
        print('%s: %s' % (self.__name, self.__score))

    def get_grade(self):
        if self.__score >= 60:
            return 'pass'
        else:
            return 'fail'

    def get_name(self):
        return self.__name

    def get_score(self):
        return self.__score


bart = Student('Bart', 60)
lisa = Student('Lisa', 57)

#bart.print_score()
#lisa.print_score()

print(bart)
print(bart.get_name())
print(bart.get_grade())


class Animal(object):
    def run(self):
        print('Animal is running...')

class Dog(Animal):
    def run(self):
        print('Dog is running...')

    def eat(self):
        print('Eat meat...')

class Cat(Animal):
    pass

def run_twice(animal):
    animal.run()
    animal.run()



dog = Dog()
dog.run()
dog.eat()

run_twice(Animal())

cat = Cat()
cat.run()

print(type(cat))
print(type(123))
print(type('123'))

print(type(None))

print(type(abs))

print(type(Animal()))


s = dir('123')

print([dir('123')])
